<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;


class User extends Controller
{
    public function index()
    {
        // 原生SQL
        $list = Db::query('SELECT * FROM user');
        $list = Db::query('SELECT * FROM user WHERE id = ?', [6]);

        // DB类
        $list = Db::table('user')->order(['id' => 'desc'])->select();

        // 助手函数
        $list = db('user')->field(['id', 'age'])->select();

        dump($list);
    }

    public function insert()
    {
        // 原生SQL
//        $result = Db::execute('INSERT INTO user (id,name,age) VALUES (null,"aa1", 36)');
//        $result = Db::execute('INSERT INTO user (id,name) VALUES (:id, :name)', ['id'=>null, 'name'=>'aa2']);
        $data = [
            'name' => 'aa5',
            'age' => 77,
        ];
        // DB类
//        $result = Db::table('user')->data($data)->insert();
//        $result = Db::table('user')->insertGetId($data);

        // 助手函数
        $result = db('user')->insert($data);

        dump($result);
    }


    public function delete()
    {
        // 原生SQL
//        $result = Db::execute('DELETE FROM user WHERE name="aa4"');
        // DB类
//        $result = Db::table('user')->delete(55);
//        $result = Db::table('user')->where('name','aa1')->delete();

        // 助手函数
        $result = db('user')->where('name','aa5')->delete();
        dump($result);
    }


    public function update()
    {
        // 原生SQL
//        $result = Db::execute('UPDATE user SET age="18" WHERE id="1"');
        // DB类
//        $result = Db::table('user')->where('id','1')->update(['age'=>'18','province'=>'上海']);

        // 助手函数
        $result = db('user')->where('id','1')->update(['age'=>'18','province'=>'江苏']);
        dump($result);
    }

}
